package cn.lysj.framework.common.validate.validate;

import cn.lysj.framework.base.BaseEntity;
import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.utils.spring.SpringContextUtil;
import cn.lysj.framework.common.validate.annotation.ForeignKey;
import cn.lysj.framework.common.validate.entity.Entity;


public class ForeignKeyValidate extends AbstractValidate<ForeignKey> {

    private BaseService baseService;

    public boolean validate(Entity entity) {
        Object object = entity.getValue();
        if (null == object || "".equals(object.toString().trim())) return true;
        baseService = SpringContextUtil.getBean(annotation.service());
        BaseEntity baseEntity = baseService.findOne(String.valueOf(entity.getValue()));
        if(baseEntity == null || baseEntity.getDelFlag().equals(CommonConstant.DEL_FLAG)){
            return false;
        }
        return true;
    }

    public void init() {
        this.setMsg(annotation.message());
        this.setGroups(annotation.groups());
    }

}
